package com.kingland.springbootstarterdataredis.service;

import com.kingland.springbootstarterdataredis.mapper.StudentMapper;
import com.kingland.springbootstarterdataredis.model.StudentModel;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;


    @Override
    public List<StudentModel> findAll() {
        return studentMapper.findAll();
    }

    @Override
    @CachePut(value = "student", key = "#student.id")
    public StudentModel save(StudentModel student) {
        studentMapper.save(student);
        System.out.println("insert to postgresql the user id is: " + student.getId());
        return student;
    }

    @Override
    @Cacheable(value = "student", key = "#id")
    public StudentModel findById(Long id) {
        System.out.println("find from postgresql: " + id);
        return studentMapper.findById(id);
    }

    @Override
    @CacheEvict(value = "student", key = "#id")
    public void deleteById(Long id) {
        System.out.println("delete from postgresql: " + id);
        studentMapper.deleteById(id);
    }
}
